草庐IT

HTTP 400 错误

全部标签

go - 如何解决golang中 "missing Location in call to Date"的错误

关闭。这个问题是notreproducibleorwascausedbytypos.它目前不接受答案。这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topic在这里,这个问题的解决方式不太可能帮助future的读者。关闭4年前。Improvethisquestion我写了一个golang程序来生成不同地点的开始日期和结束日期ifmonth!=""&&year!=""{varmonthInt,_=strconv.Atoi(month)varyearInt,_=strconv.Atoi(year)timeZone,err:=time.LoadLocation("A

http.Get() 从 url 获取图像并写入 GridFS

我想从url请求图像并将该图像写入mongoDbGridFS数据库。我得到的唯一可行方法是将请求的正文保存到操作系统上的文件中,然后再次打开它。...response,err:=http.Get("https://via.placeholder.com/350x150")deferresponse.Body.Close()file,_:=os.Create("file-name-placeholder.jpg")b,_:=io.Copy(file,response.Body)file.Close()file,err=os.Open("file-name-placeholder.jpg"

go - 带有 http.SetCookie 的无效内存地址

我正在开发一个名为persona的授权包。除一件事外一切正常,当我尝试设置cookie时,我有一个无效的内存地址。funcSignup(userinterface{},usernamestring,whttp.ResponseWriter)error{key:=[]byte(randStringBytes(32))encrypted,err:=encrypt(key,username)iferr!=nil{returnerr}expiration:=time.Now().Add(365*24*time.Hour)cookie:=http.Cookie{Name:"session-per

go - 错误处理中间件抛出运行时错误

我有这个错误处理中间件:funcError(nexthttp.HandlerFunc)http.Handler{returnhttp.HandlerFunc(func(whttp.ResponseWriter,r*http.Request){deferfunc(){iferr:=recover();err!=nil{log.Error("Caughterrorindefer/recovermiddleware:",err)originalError:=err.(struct{OriginalErrorerror}).OriginalErroriforiginalError!=nil{l

go - 使用键解析 yaml 提供错误

我有以下需要解析的yaml,我试过以下Build-t:before:test1-value:dddd-bbb:zzzzafter:test2-value:bbb-aaa:aaaa我试过以下方法:typerootstruct{buildtypeBuild`yaml:"Build-t,omitempty"`}typeBuildstruct{Beforemap[string]interface{}`yaml:"before,omitempty"`Aftermap[string]interface{}`yaml:"after,omitempty"`}现在当我解析它时出现错误,我需要的是从对象b

Golang - 在方法中测试 HTTP 请求

谈到在Go中进行测试时,我有点困惑。我读过在某些情况下抽象到接口(interface)应该是理想的方式,在其他情况下我看到了TestTables。我不太确定何时应用其中任何一个。例如,如何测试下面的功能。typeUser{Namestring`json:"name"`IsMarriedbool`json:"isMarried"`Nicknames[]string`json:"nicknames"`}func(u*User)Create()(*http.Response,error){data,err:=json.Marshal(u)iferr!=nil{returnnil,err}ur

go - 使用 go 的 HTTP PUT 请求处理程序

请考虑这个示例Go代码片段,packagemainimport("fmt""log""net/http""time")funcmain(){listen_at:=":3114"gohttp.Handle("/",http.FileServer(http.Dir(".")))//gohttp.Handle("/max",http.FileServer(http.Dir(".")))我有两个问题:(1)如何更改文件服务器以提供/max而不是/-我的尝试失败了,我得到404forhttp://localhost:3114/max/和http://localhost:3114/max。(2)我

go - 如果 Println 出错,如果可以将其作为变量,如何显示 Println 的错误值?

它说:Printf根据格式说明符格式化并写入标准输出。它返回写入的字节数和遇到的任何写入错误。如果错误存储在b中,如何创建/测试错误并显示错误当我运行程序时它显示“nil”,因为没有错误,我如何显示任何错误?a,b:=fmt.Println("Hello,playground")fmt.Println(a)fmt.Println(b)https://golang.org/pkg/fmt/#PrintlnfuncPrintln(a...interface{})(nint,errerror)https://play.golang.org/p/8Cjb2Sfunx7

http - 取消 Http 处理程序请求

我有响应https请求的处理程序。在处理程序中,我调用了一个函数F1(),它执行一些应用程序逻辑并连接到mysql数据库并执行查询。我想知道如果客户端取消请求,我如何使用golang上下文包来取消Db查询。我需要将ctx传递给F1()吗?此外,即使F1()在不到4秒内返回,我现在拥有的代码也将花费4秒。如何在F1()返回后立即返回?funchandler(whttp.ResponseWriter,r*http.Request){ctx:=r.context()F1()select{case 最佳答案 首先,我强烈建议您查看Conte

go - Nginx version 命令不返回版本也不返回错误

我正在尝试使用Go检查我的计算机上安装了哪个版本的Nginx。这是我的代码片段:packagemainimport("bytes""errors""fmt""os/exec")funcrunCommand(commandstring,arg...string)(string,error){cmd:=exec.Command(command,arg...)cmdOutput:=&bytes.Buffer{}errOutput:=&bytes.Buffer{}cmd.Stdout=cmdOutputcmd.Stderr=errOutputerr:=cmd.Run()iferr!=nil{r